<?php
class StudentApprove extends Controller {
	var $msg = '';
	function StudentApprove()
	{
		parent::Controller();
	}

	
	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$data['form_status'] = 'add';
		$this->load->model('StudentModel');
		$data['studentlList'] = $this->StudentModel->getAllFromFrontEnd();
		$this->load->view('header');
		$this->load->view('student-approve-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	}
	
	function detail($id)
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		$data['form_status'] = 'add';
		$this->load->model('StudentModel');
		$data['studentlList'] = $this->StudentModel->getFromFrontEndByID($id);
		$this->load->view('header');
		$this->load->view('student-approve-form', $data);
		$this->load->view('footer');
	    
	}
	
	function save()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentModel');
	    $stuID = $this->input->post('txtDel');
	    $comArr = $this->input->post('comment');
	    if(isset($stuID) && empty($stuID) == false) {
	    for($i = 0; $i < count($stuID); $i++) {
	    	$r = $stuID[$i];
			$tmps = explode(",", $r);
	    	$data = array();
	    	$data['student_id'] = $tmps[0];
    		$data['status'] = 'approve';
    		if($tmps[1] != null && $comArr[$tmps[1]-1] != null)
				$d['comment'] = $comArr[$tmps[1]-1];
			else 
				$d['comment'] = null;
    		
    		$this->StudentModel->approve($data);
	    }
	    }
	    $this->msg = "บันทึกข้อมูลเรียบร้อย";
	    $this->index();
	}
	
	function ignore()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentModel');
	    $stuID = $this->input->post('txtDel');
	    $comArr = $this->input->post('comment');
	    
	    for($i = 0; $i < count($stuID); $i++) {
    		$this->StudentModel->deleteComment($stuID[$i], $comArr[$i]);
	    }
	    $this->msg = "บันทึกข้อมูลเรียบร้อย";
	    $this->index();
	}
}

?>